package com.wuming.house.controller;

import com.wuming.house.core.JsonResult;
import com.wuming.house.model.Function;
import com.wuming.house.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author xuhuai
 * @createTime 2019/9/10 16:36
 * @Description:
 */
@Controller
@RequestMapping("/web/function")
public class FunctionController extends BaseController{

    @Autowired
    private FunctionService functionService;

    @RequestMapping(value = "/toManageFunction",method = RequestMethod.GET)
    public String toManageFuntion(){
        return "/tools/manageFunction";
    }

    @ResponseBody
    @RequestMapping("/edit")
    public JsonResult edit(@RequestParam Long functionId,@RequestParam Integer functionStatus){
        Function function = new Function();
        function.setId(functionId);
        function.setFunctionStatus(functionStatus);
        functionService.modifyFunction(function);
        return new JsonResult(null,"修改功能状态成功",true);
    }

    @ResponseBody
    @RequestMapping("/getFunction")
    public JsonResult getFunction(@RequestParam Long functionId){
        Function function = functionService.getFunctionById(functionId);
        return new JsonResult(function,"查询功能成功",true);
    }

    @RequestMapping("/toSystemBanner/{functionId}")
    public String toSystemBanner(@PathVariable Long functionId, Model model){
        model.addAttribute("function",functionService.getFunctionById(functionId));
        return "/tools/systemBanner";
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request){
        try {
            String path = this.uploadImage(file,"systemBanner",1);
            logger.info("文件成功上传到指定目录下,文件目录:{},",path);
            return new JsonResult(path,"上传成功",true);
        }catch (IOException ie){
            ie.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult("","上传失败",false);
    }

    @ResponseBody
    @RequestMapping(value = "/editSystem")
    public JsonResult editSystem(Function function){
        if (null == function.getId()) {
            functionService.saveFunction(function);
            return new JsonResult(null,"添加广告配置成功");
        } else {
            functionService.modifyFunction(function);
            return new JsonResult(null,"更新广告配置成功");
        }
    }
}
